package com.hp.provider.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * spring-boot-starter-amqp func
 * <p>
 * 直连交换机，发送指定队列信息，但这个队列后有两个消费者同时进行消费
 *
 * @author <a href="mailto:xing.dong@sha.ontime-express.com">Xavier Dong</a>
 * @date
 */
@Configuration
public class DirectExchangeTxQueueConfig {

    /**
     * 一个消息转发器，我们取名directExchangeTx
     *
     * @return {@link DirectExchange}
     */
    @Bean(name = "getDirectExchangeTx")
    public DirectExchange getDirectExchangeTx() {
        return new DirectExchange("directExchangeTx", true, false);
    }

    /**
     * 一个消息队列，取名directQueueTx，并将其绑定至指定的消息转发器上
     *
     * @return {@link Queue}
     */
    @Bean(name = "getQueueTx")
    public Queue getQueueTx() {
        return new Queue("directQueueTx", true, false, false);
    }

    /**
     * 将 DirectExchange 和  Queue 进行绑定
     *
     * @param getDirectExchangeTx 直接交换tx
     * @param getQueueTx          获得队列tx
     * @return {@link Binding}
     */
    @Bean
    public Binding getDirectExchangeQueueTx(
            @Qualifier(value = "getDirectExchangeTx") DirectExchange getDirectExchangeTx,
            @Qualifier(value = "getQueueTx") Queue getQueueTx) {
        return BindingBuilder.bind(getQueueTx).to(getDirectExchangeTx).with("directQueueTxRoutingKey");
    }
}